<?php
header('Content-type: application/xls');
header('Content-Disposition:attachment;filename=thongke.xls');
?>
<?php if(!empty($thongke)){?>
<?php $tongchidoan = count($cdoansvtk)+count($cdoancbtk);?>
<h3 style=text-align:center>DANH SÁCH THỐNG KÊ CÔNG TÁC TỔ CHỨC</h3>
<?php echo 'Đơn vị:'.$tenkhoa[0] ['khoas'] ['tenkhoa'];?>
<br>
<?php echo 'Tổng số thanh niên: '. $thongke['thanhnien']?>
<br>
<?php echo 'Tổng số Đoàn viên: '.$thongke['doanvien']?>
<br>
<?php echo 'Tổng số Chi đoàn trực thuộc: '.$tongchidoan?>
<br>
<?php echo 'Chi đoàn Cán bộ: '.count($cdoancbtk);?>
<br>
<?php echo 'Chi đoàn sinh viên: '.count($cdoansvtk);?>
<br>
<h5>Danh sách các Chi đoàn:</h5>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th style="background: #d5d5d5; font-weight: bold;">STT</th>
<th style="background: #d5d5d5; font-weight: bold;">Tên chi đoàn</th>
<th style="background: #d5d5d5; font-weight: bold;">TS Thanh niên(Nữ)</th>
<th style="background: #d5d5d5; font-weight: bold;">TS Đoàn viên(Nữ)</th>
<th style="background: #d5d5d5; font-weight: bold;">TS Đảng viên</th>
</tr>
<?php $i=0;?>
<?php 
$tongnutn =0;
$tongnudv = 0;
$tongdang = 0;
?>
<?php foreach ($cdoancbtk as $key => $value):?>
<?php $i++;?>
<tr>
<td><?php echo $i?></td>
<td><?php echo $value?></td>
<td>
<?php 
		$tstncb = 0;
		$tstnnucb =0;
		foreach ($thongkethanhniencb as $thanhniencb)
		{
			if($thanhniencb['Chidoansv']['id']==$key)
			{
				$tstncb = $thanhniencb[0]['soluong'];
				break;
			}
		}
		foreach ($thongkethanhniennamnucb as $thanhniennamnucb)
		{
			if($thanhniennamnucb['Chidoansv']['id']==$key)
			{
				$tstnnucb = $thanhniennamnucb[0]['soluong'];
				break;
			}
		}
		echo $tstncb.'('.$tstnnucb.')';
		$tongnutn +=$tstnnucb;
?>
</td>
<td>
<?php 
		$tsdvcb = 0;
		$tsdvnucb=0;
		foreach ($thongkedoanviencb as $doanviencb)
		{
			if($doanviencb['Chidoansv']['id']==$key)
			{
				$tsdvcb = $doanviencb[0]['soluong'];
				break;
			}
		}
		foreach ($thongkedoanviennamnucb as $doanviennamnucb)
		{
			if($doanviennamnucb['Chidoansv']['id']==$key)
			{
				$tsdvnucb = $doanviennamnucb[0]['soluong'];
				break;
			}
		}
		
		echo $tsdvcb.'('.$tsdvnucb.')';
		$tongnudv +=$tsdvnucb;
?>
</td>
<td>
<?php
$tsdavcb =0;
foreach ($thongkedangviencb as $dangviencb)
		{
			if($dangviencb['Chidoansv']['id']==$key)
			{
				$tsdavcb = $dangviencb[0]['soluong'];
				break;
			}
		} 
		echo $tsdavcb;
		$tongdang +=$tsdavcb;
?>
</td>
</tr>
<?php endforeach;?>
<?php foreach ($cdoansvtk as $key => $value):?>
<?php $i++;?>
<tr>
<td><?php echo $i?></td>
<td><?php echo $value?></td>
<td>
<?php 
		$tstnsv = 0;
		$tstnnusv=0;
		foreach ($thongkethanhniensv as $thanhniensv)
		{
			if($thanhniensv['Chidoansv']['id']==$key)
			{
				$tstnsv = $thanhniensv[0]['soluong'];
				break;
			}
		}
		foreach ($thongkethanhniennamnusv as $thanhniennamnusv)
		{
			if($thanhniennamnusv['Chidoansv']['id']==$key)
			{
				$tstnnusv = $thanhniennamnusv[0]['soluong'];
				break;
			}
		}
		echo $tstnsv.'('.$tstnnusv.')';
		$tongnutn +=$tstnnusv;
?>
</td>
<td>
<?php 
		$tsdvsv = 0;
		$tsdvnusv=0;
		foreach ($thongkedoanviensv as $doanviensv)
		{
			if($doanviensv['Chidoansv']['id']==$key)
			{
				$tsdvsv = $doanviensv[0]['soluong'];
				break;
			}
		}
		foreach ($thongkedoanviennamnusv as $doanviennamnusv)
		{
			if($doanviennamnusv['Chidoansv']['id']==$key)
			{
				$tsdvnusv = $doanviennamnusv[0]['soluong'];
				break;
			}
		}
				echo $tsdvsv.'('.$tsdvnusv.')';
				$tongnudv +=$tsdvnusv;
?>	
</td>
<td>
<?php
$tsdavsv =0;
foreach ($thongkedangviensv as $dangviensv)
		{
			if($dangviensv['Chidoansv']['id']==$key)
			{
				$tsdavsv = $dangviensv[0]['soluong'];
				break;
			}
		} 
		echo $tsdavsv;
		$tongdang +=$tsdavsv;
?>
</td>
</tr>
<?php endforeach;?>
<tr>
<td colspan=2 style='text-align:center'> <strong>Tổng số</strong></td>
<td><?php echo $thongke['thanhnien'].'('.$tongnutn.')';?></td>
<td><?php echo $thongke['doanvien'].'('.$tongnudv.')';?></td>
<td><?php echo $tongdang;?></td>
</tr>
</table>
<?php }?>